<?php

/**
 * Created by PhpStorm.
 * User:money
 * Date: 17-6-22
 * Time: 10:00
 */

namespace  v1;

class User extends \ResourceClass{
    public $isActive = true;
    public $remark = '微信授权获取用户信息';

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['openid'])) {
            return 'require openid ';
        }
        $userclass = new \Orm\User\UserClass();
        try {
            // 查询数据库，有信息就返回
            $userclass->loadByOpenId($req['openid']);
            $result['open_id'] = $userclass['openid'];
            $result['nickname'] = $userclass['nickname'];
            $result['headimgurl'] = $userclass['headimgurl'];
            return '';
        }catch (\Exception $e) {
            if(!isset($req['access_token'])){
                return 'require access_token ';
            }
            $access_token = $req['access_token'];
            $openid = $req['openid'];
            $get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
            $userinfo = $this->http_get($get_user_info_url);
            $user_info = json_decode($userinfo,true);
            if(array_key_exists('nickname',$user_info)){ // 查询是否关注
                $userclass['open_id'] = $user_info['openid'];
                $userclass['nickname'] = $user_info['nickname'];
                $userclass['headimgurl'] = $user_info['headimgurl'];
                $userclass ->insert();
                $result = $userclass->objToArray($userclass);
                return '';
            }else {// 查询是否关注
                $result = array();
            }
        }
    }

    public function post(array $req, array $post, &$result)
    {
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        if (!isset($data['openid'])) {
            return 'openid不能为空';
        }
        if (!isset($data['nickname'])) {
            return '用户名不能为空';
        }
        if (!isset($data['headimgurl'])) {
            return '头像不能为空';
        }

        try {
            $user_class = new \Orm\User\UserClass();
            $user_class['open_id'] = $data['openid'];
            $user_class['nickname'] = $data['nickname'];
            $user_class['headimgurl'] = $data['headimgurl'];
            $user_class->insert();

            $result = array(
                'id' => $user_class['id'],
                'open_id' => $user_class['open_id'],
                'nickname' => $user_class['nickname'],
                'headimgurl' => $user_class['headimgurl'],
            );
        }catch (\Exception $e ) {
            return $e->getMessage();
        }
        return '';
    }

    //HTTP请求（支持HTTP/HTTPS，支持GET/POST）
    public function http_get($url)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }
}